package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.google.gson.Gson;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.SignUpResponse;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;

public class SignUpActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener, SignUp1Fragment.SignUpGetCodeListener, SignUp2Fragment.SignUpCheckCodeListener, SignUp3Fragment.SignUpListener {

    public static final String PHONE = "phone";

    public static final String CODE = "code";

    private static final int STATE_SIGN_1 = 1;

    private static final int STATE_SIGN_2 = 2;

    private static final int STATE_SIGN_3 = 3;

    private int mState;

    private CuteToolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        mToolbar = (CuteToolbar) findViewById(R.id.toolbar);
        mToolbar.setOnClickCuteToolbarListener(this);
        setSignUpTitle(R.string.title_sign_up_1);
        switchSignUp1();
    }

    @Override
    public void onBackPressed() {
        switch (mState) {
            case STATE_SIGN_1:
                super.onBackPressed();
                break;
            case STATE_SIGN_2:
                popBackStack(STATE_SIGN_1);
                setSignUpTitle(R.string.title_sign_up_1);
                break;
            case STATE_SIGN_3:
                popBackStack(STATE_SIGN_2);
                setSignUpTitle(R.string.title_sign_up_2);
                break;
        }
    }

    @Override
    public void onClickLeft() {
        onBackPressed();
    }

    @Override
    public void onClickRight() {

    }

    @Override
    public void signUpGetCodeSuccess(String phone) {
        setSignUpTitle(R.string.title_sign_up_2);
        switchSignUp2(phone);
    }

    @Override
    public void signUpCheckCodeSuccess(String phone, String code) {
        setSignUpTitle(R.string.title_sign_up_3);
        switchSignUp3(phone, code);
    }

    @Override
    public void signUpSuccess(SignUpResponse response, String phone) {
        LoginInfo login = new LoginInfo();
        login.setUserID(response.getId());
        login.setPhone(phone);
        login.setToken(response.getToken());
        sendSignUpBroadcast(login);
        finish();
    }

    // 设置标题
    private void setSignUpTitle(int resId) {
        mToolbar.setCuteTitle(resId);
    }

    // 切换到SignUp1
    private void switchSignUp1() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        SignUp1Fragment signUp1Fragment = new SignUp1Fragment();
        signUp1Fragment.setSignUpGetCodeListener(this);
        ft.replace(R.id.content, signUp1Fragment);
        ft.commit();
        mState = STATE_SIGN_1;
    }

    // 切换到SignUp2
    private void switchSignUp2(String phone) {
        Bundle bundle = new Bundle();
        bundle.putString(PHONE, phone);
        SignUp2Fragment signUp2Fragment = new SignUp2Fragment();
        signUp2Fragment.setSignUpCheckCodeListener(this);
        switchFragment(signUp2Fragment, bundle, STATE_SIGN_2);
    }

    // 切换到SignUp3
    private void switchSignUp3(String phone, String code) {
        Bundle bundle = new Bundle();
        bundle.putString(PHONE, phone);
        bundle.putString(CODE, code);
        SignUp3Fragment signUp3Fragment = new SignUp3Fragment();
        signUp3Fragment.setSignUpListener(this);
        switchFragment(signUp3Fragment, bundle, STATE_SIGN_3);
    }

    private void switchFragment(Fragment fragment, Bundle bundle, int state) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        fragment.setArguments(bundle);
        ft.replace(R.id.content, fragment);
        ft.addToBackStack(null);
        ft.commit();
        mState = state;
    }

    private void popBackStack(int state) {
        mState = state;
        getSupportFragmentManager().popBackStack();
    }

    private void sendSignUpBroadcast(LoginInfo login) {
        Intent intent = new Intent(Config.ACTION_SIGN_UP_SUCCESS);
        Gson gson = new Gson();
        intent.putExtra(Config.ACTION_EXTERNAL_SIGN_UP_SUCCESS_LOGIN_INFO, gson.toJson(login));
        sendBroadcast(intent);
    }

}
